package org.jkuang.qstar.commons.io;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author jkuang
 * 
 */
public class FileOperator {

	public static List<String> readFile(File file) throws IOException {
		List<String> words = new ArrayList<String>();
		FileInputStream inStream = null;
		InputStreamReader inReader = null;
		BufferedReader bufferedReader = null;
		try {
			inStream = new FileInputStream(file);
			inReader = new InputStreamReader(inStream, "utf8");
			bufferedReader = new BufferedReader(inReader);
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				if (line.length() == 1) {
					System.out.println(line);
				}
				words.add(line.trim());
			}
		} finally {
			if (bufferedReader != null) {
				bufferedReader.close();
			}
			if (inStream != null) {
				inStream.close();
			}
			if (inReader != null) {
				inReader.close();
			}
		}
		return words;
	}

	public boolean createFile(InputStream in, String outPath) throws IOException {
		FileOutputStream fileOutput = null;
		BufferedOutputStream bufferOut = null;
		try {
			fileOutput = new FileOutputStream(outPath);
			bufferOut = new BufferedOutputStream(fileOutput);
			if (in != null) {
				int len;
				byte[] data = new byte[1024 * 10];
				while ((len = in.read(data)) != -1) {
					bufferOut.write(data, 0, len);
					bufferOut.flush();
				}
				return true;
			}
		} finally {
			if (in != null) {
				in.close();
			}
			if (fileOutput != null) {
				fileOutput.close();
			}
			if (bufferOut != null) {
				bufferOut.close();
			}
		}
		return false;
	}

	public boolean createFile(String content, String outPath) throws IOException {
		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(outPath);
			fileWriter.write(content);
			fileWriter.flush();
			return true;
		} finally {
			if (fileWriter != null) {
				fileWriter.close();
			}
		}
	}

	public boolean createFile(char[] in, String outFile) throws IOException {
		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(outFile);
			fileWriter.write(in);
			fileWriter.flush();
			return true;
		} finally {
			if (fileWriter != null) {
				fileWriter.close();
			}
		}
	}

	public boolean createFile(File in, String outPath) throws IOException {
		FileWriter fileWriter = null;
		FileReader fileReader = null;
		try {
			fileReader = new FileReader(in);
			fileWriter = new FileWriter(outPath);
			int len;
			char[] cbuf = new char[1024 * 10];
			while ((len = fileReader.read(cbuf)) != -1) {
				fileWriter.write(cbuf, 0, len);
				fileWriter.flush();
			}
			return true;
		} finally {
			if (fileWriter != null) {
				fileWriter.close();
			}
			if (fileReader != null) {
				fileReader.close();
			}
		}
	}

	public void createFile(String content, String fileName, String encoding) throws IOException {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		try {
			fos = new FileOutputStream(fileName);
			osw = new OutputStreamWriter(fos, encoding);
			osw.write(content);
			osw.flush();
		} finally {
			if (fos != null) {
				fos.close();
			}
			if (osw != null) {
				osw.close();
			}
		}
	}

	public void createFile(InputStream in, String inEncoding, String outPath, String outEncoding) throws IOException {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		InputStreamReader inputReader = null;
		try {
			inputReader = new InputStreamReader(in, inEncoding);
			fos = new FileOutputStream(outPath);
			osw = new OutputStreamWriter(fos, outEncoding);
			int len;
			char[] cbuf = new char[1024 * 10];
			while ((len = inputReader.read(cbuf)) != -1) {
				osw.write(cbuf, 0, len);
				osw.flush();
			}
		} finally {
			if (fos != null) {
				fos.close();
			}
			if (osw != null) {
				osw.close();
			}
			if (inputReader != null) {
				inputReader.close();
			}
		}
	}

	public void createFile(File in, String inEncoding, File out, String outEncoding) throws IOException {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		FileInputStream inputStream = null;
		InputStreamReader inputReader = null;
		try {
			inputStream = new FileInputStream(in);
			inputReader = new InputStreamReader(inputStream, inEncoding);
			fos = new FileOutputStream(out);
			osw = new OutputStreamWriter(fos, outEncoding);
			int len;
			char[] cbuf = new char[1024 * 10];
			while ((len = inputReader.read(cbuf)) != -1) {
				osw.write(cbuf, 0, len);
				osw.flush();
			}
		} finally {
			if (fos != null) {
				fos.close();
			}
			if (osw != null) {
				osw.close();
			}

			if (inputStream != null) {
				inputStream.close();
			}
			if (inputReader != null) {
				inputReader.close();
			}
		}
	}

	public String readFile(String filePath, String encoding) throws IOException {
		File file = new File(filePath);
		return readFile(file, encoding);
	}

	public String readFile(File file, String encoding) throws IOException {
		StringBuffer fileContent = new StringBuffer();
		FileInputStream inStream = null;
		InputStreamReader inReader = null;
		BufferedReader bufferedReader = null;
		try {
			inStream = new FileInputStream(file);
			inReader = new InputStreamReader(inStream, encoding);
			bufferedReader = new BufferedReader(inReader);
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				fileContent.append(line);
			}
		} finally {
			if (bufferedReader != null) {
				bufferedReader.close();
			}
			if (inStream != null) {
				inStream.close();
			}
			if (inReader != null) {
				inReader.close();
			}
		}
		return fileContent.toString();
	}

	/**
	 * 将str 写入文件
	 * 
	 * @param str
	 * @param fileName
	 */
	public void WriteFile(String str, String fileName) {
		try {
			File f = new File(fileName);
			if (!f.exists()) {
				f.createNewFile();
			}
			BufferedWriter output = new BufferedWriter(new FileWriter(f));
			output.write(str);

			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public byte[] readFileByBytes(String fileName) {
		InputStream in = null;
		try {
			in = new FileInputStream(fileName);
			byte[] tempbytes = new byte[in.available()];
			in.read(tempbytes);
			return tempbytes;
		} catch (Exception e1) {
			e1.printStackTrace();
			return null;
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	/**
	 * 以行为单位读取文件，常用于读面向行的格式化文件
	 */
	public String readFileByLines(String fileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			StringBuffer buffer = new StringBuffer();
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				buffer.append(tempString);
			}
			return buffer.toString();

		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}

}
